﻿@model XRM.Security.WebUI.Models.Request.RequestCertModel
@{
    ViewBag.Title = "Информация о сертификате";
    Layout = "~/Views/Shared/_LayoutDialog.cshtml";
}

<div class="modal-body" style="height:700px;padding-left:20px;overflow-y:auto;">
    @using (Html.BeginForm("CertDialog", "Request", FormMethod.Post, new { id = "dlgCertDialogForm", @class = "form-horizontal form-horizontal-2" }))
    {
        @Html.AntiForgeryToken()
        const string inputCss = "form-control";
        const string divCss = "col-sm-8";

        @Html.HiddenFor(m => m.Id)
        @Html.HiddenFor(m => m.FileName)
        @Html.HiddenFor(m => m.UserRegisterId)
        @Html.HiddenFor(m => m.IdentityCenterId)
        @Html.HiddenFor(m => m.SerialNumber)
        @Html.HiddenFor(m => m.Thumbprint)
        @Html.HiddenFor(m => m.FromDate)
        @Html.HiddenFor(m => m.ToDate)
        @Html.HiddenFor(m => m.Status)
                
            <div class="form-group">
                @Html.LabelFor(m => m.IdentityCenterId, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("tbIdentityCenterName", Model.CertIdentityCenter.IdentityCenterName, new { @class = @inputCss, disabled = "disabled" })
                </div>
            </div>
        
            <div class="form-group">
                @Html.LabelFor(m => m.SerialNumber, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("tbSerialNumber", Model.SerialNumber, new { @class = @inputCss, disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.ValidPeriod, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("tbValidPeriod", Model.ValidPeriod, new { @class = @inputCss, disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.UserRegisterId, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("tbUserFullName", Model.CertUser.FullName, new { @class = @inputCss, disabled = "disabled"})
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.SerialNumberEtoken, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.TextBoxFor(m => m.SerialNumberEtoken, new { @class = @inputCss })
                    @Html.ValidationMessageFor(m => m.SerialNumberEtoken)
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.StatusCaption, new { @class = "control-label required" })
                <div class="@divCss">
                    @Html.TextBox("tbStatusCaption", Model.StatusCaption, new { @class = @inputCss, disabled = "disabled" })
                </div>
            </div>
            <div class="form-group">
                @Html.LabelFor(m => m.PCId, new { @class = "control-label" })
                <div class="@divCss">
                    @Html.DropDownListFor(m => m.PCId, new SelectList(Model.CertUser.PCList, "Id", "SerialNumber"), new { @class = @inputCss })
                </div>
            </div>
        
            <div class="form-group">
                @Html.LabelFor(m => m.InfoSystemList, new { @class = "control-label" })
                <div class="@divCss" id="infosystemArray">
                    <div data-bind="template: { name: 'infosystem-template', foreach: InfoSystem }"></div>
                    <div class="control-text">
                        <a href="#" data-bind="click: addInfoSystem">Добавить информационную систему</a>
                    </div>
                </div>
            </div>
            
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" id="btnSave">Сохранить</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
            </div>
        
    }
</div>

<script type="text/html" id="infosystem-template">
    <div class="input-list-item" data-bind="css: { 'input-group': $index() >= 0 }">
        <select class="form-control" name="InfoSystemList[].Id" data-bind="attr: { name: 'InfoSystemList[' + $index() + '].Id' }, options: InfoSystemAll, optionsText: 'name', optionsValue: 'id', value: selectedValue"  />
        <span class="input-group-btn" data-bind="">
            <button title="Удалить информационную систему" class="btn btn-default" type="button" data-bind="click: $parent.removeInfoSystem">
                <i class="fa fa-times"></i>
            </button>
        </span>
    </div>
</script>

<script type="text/javascript" src="@(Url.Content("~/Scripts/AjaxRequest.js"))"></script>
<script type="text/javascript">
        $(document).ready(function () {

        });
    </script>
    <script src="@Url.Content("~/Scripts/knockout-3.2.0.js")"></script>
    <script type="text/javascript">
        function CertRegisterViewModel() {
            var self = this;

            self.InfoSystemAll = ko.observableArray([
                @{
                    if ((Model.CertUser.InfoSystemList != null) && (Model.CertUser.InfoSystemList.Any()))
                    {
                        foreach (var item in Model.CertUser.InfoSystemList)
                        {
                            <text>{ id: '@item.InfoSystem.Id', name: '@item.InfoSystem.InfoSystemName.Replace("\\", "\\\\")' },</text>
                        }
                    }
                    else
                    {
                        <text>{ id: '-1', name: '' }</text>
                    }
                }
            ]);

            self.InfoSystem = ko.observableArray([
                @{
                    if ((Model.InfoSystemList != null) && (Model.InfoSystemList.Any()))
                    {
                        foreach (var item in Model.InfoSystemList)
                        {
                            <text>{ id: '@item.Id', name: '@item.InfoSystemName.Replace("\\", "\\\\")', InfoSystemAll: self.InfoSystemAll, selectedValue: '@item.Id' },</text>
                        }
                    }
                    else
                    {
                        <text></text>
                    }
                }
            ]);

            self.addInfoSystem = function () {
                self.InfoSystem.push({ id: "1", name: "", InfoSystemAll: self.InfoSystemAll, selectedValue: '-1' });
            };

            self.removeInfoSystem = function () {
                self.InfoSystem.remove(this);
            };
        }

        koModel = new CertRegisterViewModel();
        ko.applyBindings(koModel, document.getElementById("infosystemArray"));
</script> 
